/*
 * =====================================================================================
 *
 *       Filename:  simple_udp_conn_cli.cpp
 *
 *    Description:  简单的UDP客户端，发送之前要连接
 *
 *        Version:  1.0
 *        Created:  2008年09月04日 21时04分41秒
 *       Revision:  none
 *       Compiler:  g++
 *
 *         Author:  ZhixiongHong (hzx), fishwarter@gmail.com
 *        Company:  Unioncast
 *
 * =====================================================================================
 */

#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

#include <ctime>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

#define MAX_LINE 	1024
#define SEND_TIMES	(1024*1024*1024)

void comm(int sockfd, sockaddr* pserver, int len);

int main(int argc, char** argv)
{
	if( argc != 3)
	{
		cout << "useage: " << argv[0] << " host  port " << endl;
		return -1;
	}
	
	unsigned short port = atoi(argv[2]);
	
	struct sockaddr_in	serverAddr;
	
	int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if( sockfd < 0 )
	{
		cout << "can't create socket " << endl;
		return -1;
	}
	
	bzero(&serverAddr, sizeof(serverAddr));
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(port);
	inet_pton(AF_INET, argv[1], &serverAddr.sin_addr);
	
	connect(sockfd, (sockaddr* )&serverAddr, sizeof(serverAddr));			
	
	comm(sockfd, (sockaddr* )&serverAddr, sizeof(serverAddr));
	return 0;
}


void comm(int sockfd, sockaddr* pserver, int len)
{

	time_t t1 = time(NULL);
	char* buf = "bni ge fsfsfs  sf sf sf s fs fsd f sdf ";
	for(int i = 0; i < SEND_TIMES; i++)
	{
		sendto(sockfd, buf, strlen(buf), 0, pserver, len);
	}
	time_t t2 = time(NULL);
	
	cout << "t2 - t1 = " << t2 - t1 << endl;
	
}


